package com.reptile.lottery.service;


import com.reptile.lottery.dao.SportsLottoDao;
import com.reptile.lottery.dao.TwoColorBallDao;
import com.reptile.lottery.show.SportsLotto;
import com.reptile.lottery.show.TwoColorBall;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * Description:
 * Author:zhouyongqiang
 * Date: 2020/12/22 15:33
 * Copyright (c) 2020,All Rights Reserved.
 */
@Service
public class TwoColorBallService {
    @Autowired
    private TwoColorBallDao twoColorBallDao;

    public List<TwoColorBall> getAllTwoColorBalls() {
        return twoColorBallDao.findAll();
    }

    public void searchAllTwoColorBalls() {
        OkHttpClient client = new OkHttpClient();
        String url = "https://kaijiang.500.com/ssq.shtml";
        String content = null;
        try {
            content = client.newCall(new Request.Builder().url(url).get().build()).execute().body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Source iSelectBox = new Source(content);
        Element table = iSelectBox.getFirstElementByClass("iSelectBox");
        Element selectBoxs = table.getFirstElement(HTMLElementName.SPAN).getFirstElement();

        Source iSelectSource = new Source(selectBoxs.toString());
        Element iSelectTable = iSelectSource.getFirstElementByClass("iSelectList");
        Element selectList = iSelectTable.getFirstElement(HTMLElementName.DIV).getFirstElement();

        Source hrefSource = new Source(selectList.toString());
        List<Element> hrefList = hrefSource.getAllElements(HTMLElementName.A);
        for (int i = 0; i < hrefList.size(); i++) {
            Element hrefElement = hrefList.get(i);
            Integer number = Integer.valueOf(hrefElement.getContent().toString());
            String href = hrefElement.getAttributeValue("href");
            TwoColorBall twoColorBall = twoColorBallDao.findByNumber(number);
            if (twoColorBall == null) {
                addNumber(number, href);
            }
        }
    }

    public void addNumber(Integer number, String href) {
        OkHttpClient client = new OkHttpClient();
        String url = href;
        String content = null;
        try {
            content = client.newCall(new Request.Builder().url(url).get().build()).execute().body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Source isContent = new Source(content);
        Element ballBoxTable = isContent.getFirstElementByClass("ball_box01");
        Element ballBox = ballBoxTable.getFirstElement(HTMLElementName.DIV).getFirstElement();
        Element ul = ballBox.getFirstElement(HTMLElementName.UL).getFirstElement();
        List<Element> allElements = ul.getAllElements(HTMLElementName.LI);
        for (int i = 0; i < 7; i++) {
            String red1 = allElements.get(0).getContent().toString();
            String red2 = allElements.get(1).getContent().toString();
            String red3 = allElements.get(2).getContent().toString();
            String red4 = allElements.get(3).getContent().toString();
            String red5 = allElements.get(4).getContent().toString();
            String red6 = allElements.get(5).getContent().toString();
            String blue1 = allElements.get(6).getContent().toString();
            TwoColorBall twoColorBall = new TwoColorBall();
            twoColorBall.setNumber(number);
            twoColorBall.setRed1(red1);
            twoColorBall.setRed2(red2);
            twoColorBall.setRed3(red3);
            twoColorBall.setRed4(red4);
            twoColorBall.setRed5(red5);
            twoColorBall.setRed6(red6);
            twoColorBall.setBlue1(blue1);
            twoColorBallDao.save(twoColorBall);
        }
    }

    public static void main(String[] args) {
        //searchAllTwoColorBalls();
        System.out.println();
    }
}
